dofile (getScriptPath() .. "\\..\\processor\\processor.lua")
--dofile (getScriptPath() .. "\\..\\processor\\watchdog.lua")
dofile (getScriptPath() .. "\\..\\processor\\log.lua")
dofile (getScriptPath() .. "\\..\\processor\\notify.lua")

--dofile (getScriptPath() .. "\\..\\lib\\actions.lua")
dofile (getScriptPath() .. "\\..\\lib\\lib.lua")
--dofile (getScriptPath() .. "\\..\\lib\\orders.lua")

script_name = string.gsub(string.gsub(debug.getinfo(1).short_src, "%.[^%.]*$", ""), "^.*[\\/]", "")

daily_exit = {
}

processor:AddInit(function()
	client_code = lib:GetUser()
    if not client_code then return true end
	script_id = client_code .. "#" .. script_name
	log_file = lib.path.log .. script_id .. ".log"
end)

processor:AddInit(function()
	processor:InitLog(log_file)
	--processor:InitWatchdog(script_id)
end)

processor:AddInit(function()
	processor:log("started")
end)

processor:AddTimer(function(t)
	if not lib:isConnected() then return true end
	
	if daily_exit.trade_date ~= lib:FormatDate() then
		daily_exit.trade_date = lib:FormatDate()
		daily_exit.end_time = os.sysdate()
		daily_exit.end_time.hour = 18
		daily_exit.end_time.min = 45
		daily_exit.end_time.sec = 0
		daily_exit.end_time.ms = 0
		daily_exit.end_time.mcs = 0
		
		daily_exit.end_time = lib:tickseconds(daily_exit.end_time)
		
		lib:GetInstrument()
	end
	
	if daily_exit.end_time and daily_exit.end_time - 300 <= lib:tickseconds() and daily_exit.end_time > lib:tickseconds() then
		daily_exit.end_time = nil
		daily_exit:DoEnd()
	end
end)
--[[
processor:AddTimer(function()
	if daily_exit.g_count then
		daily_exit.g_count = daily_exit.g_count - 1
		if daily_exit.g_count <= 0 then
			daily_exit.g_count = nil
		end
	else
		collectgarbage("collect")
		daily_exit.g_count = 10
	end
end)
--]]
--[[
processor:AddTimer(function(t)
	if not lib:isConnected() then return true end
	
	if daily_exit.trade_date ~= lib:FormatDate() then
		daily_exit.trade_date = lib:FormatDate()
		for i = 1, 2 do
			daily_exit[i] = {}
			daily_exit[i].end_time = os.sysdate()
			daily_exit[i].end_time.min = 45
			daily_exit[i].end_time.sec = 0
			daily_exit[i].end_time.ms = 0
			daily_exit[i].end_time.mcs = 0
		end
		
		daily_exit[1].end_time.hour = 18
		daily_exit[2].end_time.hour = 23
		
		for i = 1, #daily_exit do
			daily_exit[i].end_time = lib:tickseconds(daily_exit[i].end_time)
			daily_exit[i].exit_time = daily_exit[i].end_time - math.random(360, 600)
			--message("End " .. daily_exit[i].end_time .. " Exit " .. daily_exit[i].exit_time .. " " .. math.random(360, 600))
		end
		
		for i = 1, #daily_exit do
			if daily_exit[i].end_time <= lib:tickseconds() then
				daily_exit[i].exit_time = nil
			end
		end
	end
	
	for i = 1, #daily_exit do
		if daily_exit[i].exit_time and daily_exit[i].exit_time <= lib:tickseconds() then
			--processor:log("Exit Time")
			daily_exit[i].exit_time = nil
			daily_exit:Exit()
		end
	end
end)

function daily_exit:Exit(instrument)
	if type(instrument) == "string" then
		instrument = lib:GetInstrument(instrument)
		if not instrument then return end
	end
	
	if instrument then
		processor:notify("EXIT " .. instrument.code)
	else
		processor:notify("QUIT")
	end
	
	actions:Exit(instrument)
end
--]]
function daily_exit:DoEnd()
	lib:GetInstrument()
	
	local pl, rc = lib:GetPL()
	processor:notify(message(string.format(lib:GetUser() .. " P/L %.2f%% %.0f", rc, pl)))
	
	local val = {}
	local v
	
	for k in pairs(lib.securities) do
		v = lib:getParamEx(k, "valtoday")
		if v and v > 0 then
			table.insert(val, {0, k, v})
		end
	end
	
	if #val < 1 then
		fail("No quotes to save")
		return
	end
	
	table.sort(val, function(a,b) return a[3] > b[3] or a[3] == b[3] and a[2] < b[2] end)
	
	for i, str in ipairs(val) do
		str[1] = i
	end
	
	table.insert(val, 1, {"", "Instrument code", "Val. today"})
	
	if lib:SaveFile(lib.path.init .. "quotes." .. lib:GetUser() .. ".txt", val) then
		message(#val .. " quotes saved")
	else
		fail("Save quotes error")
		return
	end
	
	val = lib:LoadQuotes(lib.path.init .. "quotes." .. lib:GetUser() .. ".txt", nil, nil, lib.path.init .. "load_quotes." .. lib:GetUser() .. ".txt")
	if val then
		message(#val .. " quotes are filtered to load")
	else
		fail("Load quotes error")
		return
	end
end


